package com.cskaoyan.wordmemorize.controller.admin.voc;

import com.cskaoyan.wordmemorize.aop.MyOperateLog;
import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.constant.SystemPermissions;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocBookDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocBookCommand;
import com.cskaoyan.wordmemorize.request.VocBookPageRequest;
import com.cskaoyan.wordmemorize.service.VocBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/voc")

public class BookController {
    // 获取 Service 用于访问数据库的操作
    @Autowired
    VocBookService vocBookService;

    /**
     * POST 添加书籍 "/admin/voc/book"
     * 添加书籍后，书籍类目下的book_count也要变换
     *
     * @param vocBookCommand
     * @return
     */
    @PostMapping("book")
    @SaAdminCheckPermission({SystemPermissions.Book.ADD})
    @MyOperateLog(dateType = "书籍" ,operateType = 1)
    public Result addBook(@RequestBody VocBookCommand vocBookCommand) {
        Long b = vocBookService.addVocBook(vocBookCommand);
        return Result.ok(b);
    }

    // PUT 修改书籍 "/admin/voc/book"
    // voc_book表中包含了单词，以及章节数量，因此在章节，单词发生变化时，可能voc_book中的章节，单词数量也要发生变化，对象传参根据主键更新数据
    @PutMapping("book")
    @SaAdminCheckPermission(SystemPermissions.Book.UPDATE)
    public Result upDataVocBook(@RequestBody VocBookCommand vocBookCommand) {
        // 根据获取的数据修改书籍内容
        vocBookService.updateVocBook(vocBookCommand);
        return Result.ok();
    }

    /**
     * GET 获取书籍列表 "/admin/voc/book/list"
     * 分页查询请求有多种情况:
     * 查询类目下的书籍，
     * 查询类目下包含关键字的书籍,
     * 两个条件都符合的书籍
     *
     * @param pageQuery
     * @return
     */
    @GetMapping("book/list")
    @SaAdminCheckPermission(SystemPermissions.Book.SEARCH)
    public Result<PageDTO> getVocBookList(VocBookPageRequest pageQuery){
        PageDTO<VocBookDTO> vocBookList = vocBookService.getBookPage(pageQuery);
        return Result.ok(vocBookList);
    }

    // DELETE 批量删除书籍 "/admin/voc/books"
    @DeleteMapping("books")
    @SaAdminCheckPermission(SystemPermissions.Book.BATCH_DELETE)
    public List<DeleteBookOperateDTO> deleteBooksByIds(@RequestBody BatchDeleteCommand command) {
        List<DeleteBookOperateDTO> vocBookDOS = vocBookService.deleteVocBooks(command);
        return vocBookDOS;
    }

    // DELETE 删除指定书籍 "/admin/voc/book/{bookId}"
    @SaAdminCheckPermission(SystemPermissions.Book.DELETE)
    @DeleteMapping("book/{bookId}")
    public DeleteBookOperateDTO deleteBookById(@PathVariable("bookId") Long bookId) {
        DeleteBookOperateDTO deleteBookOperateDTO = vocBookService.deleteVocBook(bookId);
        return deleteBookOperateDTO;
    }

}
